package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.AttrValues;
import com.ktjiaoyu.mapper.AttrValueMapper;
import com.ktjiaoyu.service.AttrValueService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttrValueServiceImpl implements AttrValueService {
    private static final Logger LOGGER= LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Autowired
    AttrValueMapper attrValueMapper;

    @Override
    public Page<AttrValues> getPage(AttrValues entity, Integer offset, Integer pageSize) {
        Page<AttrValues> page=null;
        try{
            page =new Page<AttrValues>();
            page.setPageSize(pageSize);
            Long count=attrValueMapper.count();
            page.setTotalCount(count.intValue());
            page.setPageNo(offset);
            List<AttrValues> data=attrValueMapper.getPage(entity,page.offset(),page.getPageSize());
            page.setData(data);
        }catch (Exception e){
            LOGGER.error("AttrValueServiceImpl.findPage(SysUser,Integer,Integer)方法出现异常.",e);
            throw e;
        }
        return page;
    }

    @Override
    public AttrValues getById(Integer id) {
        try{
            return attrValueMapper.getById(id);

        }catch (Exception e){
            LOGGER.error("执行SysRoleServiceImpl.findAll()方法异常.",e);
            throw e;
        }
    }

    @Override
    public int delete(Long id) {
        try{
            return attrValueMapper.delete(id);

        }catch (Exception e){
            LOGGER.error("AttrValueServiceImpl.findAll()方法异常.",e);
            throw e;
        }
    }

    @Override
    public boolean saveOrUpdate(AttrValues attrValues) throws Exception {
        try{
            if(attrValues.getId()==null){
                return attrValueMapper.insert(attrValues)>0;
            }else {
                Long count=attrValueMapper.update(attrValues);
                return count>0;
            }
        }catch (Exception e){
            LOGGER.error("AttrValueServiceImpl.savOrUpdate(sysRole)方法出现异常.",e);
            throw e;
        }
    }

    @Override
    public Long count() {
        try{
            return attrValueMapper.count();

        }catch (Exception e){
            LOGGER.error("AttrValueServiceImpl.findAll()方法异常.",e);
            throw e;
        }
    }
}
